package grape.attend.service.group.api;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import grape.attend.service.group.po.GroupUserRel;
import grape.common.service.common.IBaseRelService;
import grape.common.service.common.IBaseService;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 考勤组与用户关系表，一对多 服务类
 * </p>
 *
 * @author yangwei
 * @since 2020-05-08
 */
public interface IGroupUserRelService extends IBaseRelService<GroupUserRel> {

    /**
     * 获取考勤组绑定的用户
     * @param groupId
     * @return
     */
    default List<GroupUserRel> getByGroupId(String groupId){
        if (groupId == null) {
            return null;
        }
        return list(Wrappers.<GroupUserRel>lambdaQuery().eq(GroupUserRel::getGroupId,groupId));
    }

    /**
     * 清空考勤组下的用户
     * @param groupId
     * @return
     */
    default boolean removeByGroupId(String groupId){
        assertParamNotEmpty(groupId,"groupId is not empty");
        return remove(Wrappers.<GroupUserRel>lambdaQuery().eq(GroupUserRel::getGroupId, groupId));
    }
    /**
     * 考勤组绑定用户
     * @param groupId 考勤组id
     * @param checkedUserIds 已选择的用户id
     * @param uncheckedUserIds 未选择的用户id
     * @param isLazyLoad 标识页面的可选择数据是否为懒加载
     */
    default void groupAssignUser(String groupId, List<String> checkedUserIds,List<String> uncheckedUserIds,Boolean isLazyLoad){

        removeAssignRel(groupId,checkedUserIds,uncheckedUserIds,isLazyLoad,GroupUserRel::getGroupId,GroupUserRel::getUserId);
        // 再添加
        doBind(groupId, checkedUserIds, true);
    }

    /**
     * 获取用户绑定的考勤组
     * @param userId
     * @return
     */
    default List<GroupUserRel> getByUserId(String userId){
        if (userId == null) {
            return null;
        }
        return list(Wrappers.<GroupUserRel>lambdaQuery().eq(GroupUserRel::getUserId,userId));
    }
    /**
     * 清空用户下的考勤组
     * @param userId
     * @return
     */
    default boolean removeByUserId(String userId){
        assertParamNotEmpty(userId,"userId is not empty");
        return remove(Wrappers.<GroupUserRel>lambdaQuery().eq(GroupUserRel::getUserId, userId));
    }
    /**
     * 用户绑定考勤组
     * @param userId
     * @param checkedGroupIds
     * @param uncheckedGroupIds
     */
    default void userAssignGroup(String userId, List<String> checkedGroupIds,List<String> uncheckedGroupIds,Boolean isLazyLoad){
        removeAssignRel(userId,checkedGroupIds,uncheckedGroupIds,isLazyLoad,GroupUserRel::getUserId,GroupUserRel::getGroupId);
        // 再添加
        doBind(userId, checkedGroupIds, false);
    }

    /**
     * 添加已选数据
     * @param mainId
     * @param checkedIds
     * @param isGroupMain
     */
    default void doBind(String mainId,List<String> checkedIds,boolean isGroupMain){
        if (!isEmpty(checkedIds)) {
            List<GroupUserRel> insert = new ArrayList<>(checkedIds.size());
            GroupUserRel groupUserRel = null;
            for (String checkedId : checkedIds) {
                groupUserRel = new GroupUserRel();
                if (isGroupMain) {
                    groupUserRel.setGroupId(mainId);
                    groupUserRel.setUserId(checkedId);
                }else {
                    groupUserRel.setGroupId(checkedId);
                    groupUserRel.setUserId(mainId);
                }
                insert.add(groupUserRel);
            }
            saveBatch(insert);
            List<String> userIds = new ArrayList<>();
            if (isGroupMain) {
                userIds.addAll(checkedIds);
            }else {
                userIds.add(mainId);
            }
            checkClasse(userIds);
        }
    }

    /**
     * 检查每一个userId的班次时间是否有交叉
     */
    void checkClasse( List<String> userIds);
}
